﻿using Common.XmlHandle;
using System;
using System.Collections.Generic;

namespace Common
{
    public class ConvertData
    {
        public List<ClassBrigade> liBrigade;
        public  List<ClassContact> liContact;
        public List<ClassConfig> liUser;
        public ClassBrigade brigade0 = new ClassBrigade();
        public ClassContact contact0 = new ClassContact();

        /// <summary>
        /// ClassData 与 ClassDataPlus 相互转换类
        /// </summary>
        /// <param name="rootPath">xml文档储存的路径</param>
        public ConvertData(string rootPath)
        {
            liBrigade = new XmlBrigade(rootPath + "\\XmlBrigade.xml").GetAll();
            liUser = new XmlUsersData(rootPath + "\\XmlUsersData.xml").GetAllUsers();
            liContact = new XmlContact(rootPath + "\\XmlContact.xml").GetAll();
        }

        public List<ClassDataPlus> DataList2Plus(List<ClassData> list)
        {
            List<ClassDataPlus> b = new List<ClassDataPlus>(100);
            foreach(ClassData data in list)
            {
                b.Add(this.Data2Plus(data));
            }
            return b;
        }

        public ClassDataPlus Data2Plus(ClassData data)
        {
            ClassDataPlus b = new ClassDataPlus();
            b.字号 = data.ID;
            b.编码 = data.Code;
            ClassBrigade bri = this.GetBrigadeByGuid(data.Brigade);
            b.市 = bri.City;
            b.区县 = bri.District;
            b.大队 = bri.Brigade;
            ClassConfig user = this.GetUserByGuid(data.User);
            b.初稿 = user.UserName;
            b.发生时间 = data.TimeHappen;
            b.委托时间 = data.TimeEntrust;
            b.完稿时间 = data.TimeArrange;
            b.发送时间 = DateTime.Now;
            b.发送人 = "";
            b.备注 = "";
            b.收费情况 = "";
            b.类型 = ((EnumCaseType)data.CaseType).ToString();
            b.民警 = data.Contact;
            b.鉴定项目 = this.GetProject(data.Vehicles);
            b.车辆及项目 = data.Vehicles;
            return b;
        }

        public ClassBrigade GetBrigadeByGuid(Guid uid)
        {
            try
            {
                foreach (ClassBrigade bri in this.liBrigade)
                {
                    if (uid == bri.Guid)
                    {
                        return bri;
                    }
                }
            }
            catch
            {
                return null;
            }
            return null;
        }

        public ClassConfig GetUserByGuid(Guid uid)
        {
            try
            {
                foreach (ClassConfig user in this.liUser)
                {
                    if (uid == user.UserGuid)
                    {
                        return user;
                    }
                }
            }
            catch
            {
                return null;
            }
            return null;
        }

        public string GetProject(List<ClassVehicle> list)
        {
            string b = "";
            string temp = "";
            foreach(ClassVehicle veh in list)
            {
                if (veh.Procj) { temp += "a"; };
                if (veh.Procs) { temp += "b"; };
                if (veh.Prohj) { temp += "c"; };
                if (veh.Pross) { temp += "d"; };
                if (veh.Procy) { temp += "e"; };
                if (veh.Progc) { temp += "f"; };
                if (veh.Prosx) { temp += "g"; };
            }
            if (temp.IndexOf("a") >= 0) { b += "车检、"; };
            if (temp.IndexOf("b") >= 0) { b += "车速、"; };
            if (temp.IndexOf("c") >= 0) { b += "痕迹、"; };
            if (temp.IndexOf("d") >= 0) { b += "损失、"; };
            if (temp.IndexOf("e") >= 0) { b += "成因、"; };
            if (temp.IndexOf("f") >= 0) { b += "过程、"; };
            if (temp.IndexOf("g") >= 0) { b += "属性、"; };
            if (b == null || b == "") { b = "未录入。"; };
            return b.Substring(0, b.Length - 1);
        }
    }
}
